package com.liujun.microserver.auth2.webjoin.server.oauth;

import java.util.Arrays;
import java.util.Base64;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

@Component
public class AuthorizationCodeConfiguration {

	/**
	 * doc:获取用户与密码编码后的字符信息
	 * 
	 * @param username
	 *                     用户名
	 * @param password
	 *                     密码
	 * @return 编码后的信息
	 */
	public String encodeCredentials(String username, String password) {
		String credentials = username + ":" + password;
		String encoded = new String(Base64.getEncoder().encode(credentials.getBytes()));
		return encoded;
	}

	/**
	 * doc:获取消息体部
	 * 
	 * @param authorizationCode
	 * @return
	 */
	public MultiValueMap<String, String> getBody(String authorizationCode) {
		MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();

		formData.add("grant_type", "authorization_code");
		formData.add("scope", "read_userinfo");
		formData.add("code", authorizationCode);
		formData.add("redirect_uri", "http://localhost:9001/callback");

		return formData;
	}

	/**
	 * d获取消息头
	 * 
	 * @param clientAuthentication
	 * @return
	 */
	public HttpHeaders getHeader(String clientAuthentication) {
		HttpHeaders httpHeaders = new HttpHeaders();

		httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
		httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
		httpHeaders.add("Authorization", "Basic " + clientAuthentication);

		return httpHeaders;
	}

}
